package com.syx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syx.entity.Area;
import com.syx.service.AreaService;
import com.syx.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AreaController {
    @Resource
    private AreaService areaService;

    //列表
    @RequestMapping("list.do")
    public String areList(Model model,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List list = areaService.areList();
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    //添加中转站
    @RequestMapping("addTo.do")
    public String addTo(){
        return "add";
    }

    //城市下拉
    @RequestMapping("cityList.do")
    @ResponseBody
    public Object cityList(Integer id){
        List list = areaService.cityList(id);
        return list;
    }

    //添加
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Area area, HttpSession session, MultipartFile photo){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            area.setPic(upload);
        }
        int i = areaService.add(area);
        return i;
    }

    //修改中转站
    @RequestMapping("updTo.do")
    public String updTo(Integer id){
        return "upd";
    }

    //回显
    @RequestMapping("hx.do")
    @ResponseBody
    public Object hx(Integer id){
        Map map = areaService.hx(id);
        return map;
    }

    //修改
    @RequestMapping("upd.do")
    @ResponseBody
    public Object upd(Area area, HttpSession session, MultipartFile photo){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            area.setPic(upload);
        }
        int i = areaService.upd(area);
        return i;
    }

}
